#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
【需求背景】
在产品端需要上线联系方式来源的快照，现需要对库内的数据情况进行盘点

【需求详情】
库内有接近3.2亿的联系方式，按来源展开，应该会有接近20亿的来源，我需要知道有快照的比例是多少
'''

import sys

sys.path.append('/mnt/moonbrook/Moonbrook')
from aes import contact_aes, name_aes
import os
import re
import json
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig, RobotConfig
from utils import Utils
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    week_min, week_max = Utils.get_date_range(ndays=7)

    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        self.contact_baidu_col = self.db.p_col('contact_baidu')

    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        # 解密，提取字段数据
        record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True,
                                              write_error_f=self.write_error_f)
        # ["contact", "nameId", "name", "socialCreditCode", "regNumber", "contactType", "exportTime"]
        contactChannelList = record_dic.get('contactChannelList', [])
        contact = record_dic.get('contact', '')
        nameId = record_dic.get('nameId', '')
        name = record_dic.get('name', '')
        _id = record_dic.get('_id', '')
        update_time = record_dic.get('import_update_time', '')
        create_time = record_dic.get('create_time', '')

        # 验证数据的合法性
        must_exists_values = [contactChannelList]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        if not self.is_data_legal(must_exists_values, must_has_one_exists_values):
            return save_result_dic

        # 真正业务逻辑
        if len(contactChannelList) != 1:
            return save_result_dic
        if not name:
            name = self.get_entName_by_nameId(nameId)
        page_link = contactChannelList[0].get('contactPageLink')
        dc_ls = [contact, _id, name, page_link]
        self.write_dc_f.write(
            '{}\n'.format(json.dumps(dc_ls, ensure_ascii=False)))
        self.write_dc_f.write('{}\n'.format(_id))

        return save_result_dic

    def run(self):
        # 需要统计的维度
        save_result_dic = {
            'exist_nameId_count': 0,
            'exist_nameId_dup': Utils.bloom_filter_fac(),

            'md_nameId_dup': Utils.bloom_filter_fac(),
            'md_nameId_count': Utils.bloom_filter_fac(),
            'table_count': 0,
            'week_add_count': 0,
        }

        save_result_dic['table_count'] = self.contact_baidu_col.count()
        with open("/tmp/dsf/name_and_nameId", 'r') as f:
            for line in f:
                line = line.strip()
                if line:
                    name, nameId = line.split('\t', 1)
                    is_exist = save_result_dic['md_nameId_dup'].add(nameId)
                    save_result_dic['md_nameId_count'] += 1
                    if not is_exist:
                        is_in_baidu_contact = self.contact_baidu_col.find_one({"nameId": nameId})
                        if is_in_baidu_contact:
                            save_result_dic['exist_nameId_count'] += 1
                    else:
                        if nameId in save_result_dic['exist_nameId_dup']:
                            save_result_dic['exist_nameId_count'] += 1
        save_result_dic['week_add_count'] = self.contact_baidu_col.find({'create_time': {"$lte": self.week_max, "$gte": self.week_min}},['_id']).count()
        self.send_by_robot(save_result_dic)

    def send_by_robot(self, result_dic):
        # 机器人相关
        url = RobotConfig.dsf_s2_web_hook_url
        title = RobotConfig.dsf_s2_title
        robot = DingtalkRobot(self.write_robot_f)
        mk_text = self.make_mk_text(result_dic)
        robot.main(url, title=title, mk_text=mk_text)

    def make_mk_text(self, result_dic):
        '''
        通过统计结果dict 拼接出mk_text
        @param result_dic: 统计结果dict
        @return: 发给钉钉的内容  str
        '''
        result_dic = Utils.serializable_bloom(result_dic)
        mk_text = ''
        mk_text += '''  
                **==========**  
                **库内总量：{}**  
                **周新增总量：{}**  
                **消耗名单数：{}**  
                **库内存在名单数：{}**  
                **==========**  
                '''.format(result_dic['table_count'], result_dic['week_add_count'],
                           result_dic['md_nameId_count'], result_dic['exist_nameId_count'],
                           )
        return mk_text

def run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '阿土伯错误数据'
    run(file_name)
    # scheduler_run(file_name)
